#include "../User/BSP/pwm.h"

#include "main.h"

extern TIM_HandleTypeDef htim2;

void BSP_PWM_Start(BSP_PWM_Channel_t channel) {
  switch (channel) {
    case BSP_PWM_TIM2_CH3:
      HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
      break;
    case BSP_PWM_TIM2_CH4:
      HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
      break;
    default:
#ifdef DEBUG
      while (1) __NOP();
#else
      return;
#endif
  }
}

BSP_PWM_Channel_t BSP_PWM_GetChannelHandle(BSP_PWM_Motor_t motor) {
  switch (motor) {
    case BSP_PWM_Motor_A:
      return BSP_PWM_TIM2_CH3;
    case BSP_PWM_Motor_B:
      return BSP_PWM_TIM2_CH4;
    default:
#ifdef DEBUG
      while (1) __NOP();
#else
      return BSP_PWM_TIM2_ERROR;
#endif
  }
}

void BSP_PWM_SetCompare(BSP_PWM_Channel_t channel, uint32_t value) {
  switch (channel) {
    case BSP_PWM_TIM2_CH3:
      __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, value);
      break;
    case BSP_PWM_TIM2_CH4:
      __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_4, value);
      break;
    default:
#ifdef DEBUG
      while (1) __NOP();
#else
      return;
#endif
  }
}
